/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Control;

import Control.exceptions.IllegalOrphanException;
import Control.exceptions.NonexistentEntityException;
import Control.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidades.Firma;
import Entidades.TipoTarjeta;
import Entidades.Banco;
import Entidades.Pago;
import Entidades.Tarjeta;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author USUARIO
 */
public class TarjetaJpaController implements Serializable {

    public TarjetaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tarjeta tarjeta) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Pago pagoOrphanCheck = tarjeta.getPago();
        if (pagoOrphanCheck != null) {
            Tarjeta oldTarjetaOfPago = pagoOrphanCheck.getTarjeta();
            if (oldTarjetaOfPago != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Pago " + pagoOrphanCheck + " already has an item of type Tarjeta whose pago column cannot be null. Please make another selection for the pago field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Firma firma = tarjeta.getFirma();
            if (firma != null) {
                firma = em.getReference(firma.getClass(), firma.getIdFirma());
                tarjeta.setFirma(firma);
            }
            TipoTarjeta tipo = tarjeta.getTipo();
            if (tipo != null) {
                tipo = em.getReference(tipo.getClass(), tipo.getIdTipo());
                tarjeta.setTipo(tipo);
            }
            Banco banco = tarjeta.getBanco();
            if (banco != null) {
                banco = em.getReference(banco.getClass(), banco.getIdBanco());
                tarjeta.setBanco(banco);
            }
            Pago pago = tarjeta.getPago();
            if (pago != null) {
                pago = em.getReference(pago.getClass(), pago.getNroTransaccion());
                tarjeta.setPago(pago);
            }
            em.persist(tarjeta);
            if (firma != null) {
                firma.getTarjetaList().add(tarjeta);
                firma = em.merge(firma);
            }
            if (tipo != null) {
                tipo.getTarjetaList().add(tarjeta);
                tipo = em.merge(tipo);
            }
            if (banco != null) {
                banco.getTarjetaList().add(tarjeta);
                banco = em.merge(banco);
            }
            if (pago != null) {
                pago.setTarjeta(tarjeta);
                pago = em.merge(pago);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTarjeta(tarjeta.getNroTransaccion()) != null) {
                throw new PreexistingEntityException("Tarjeta " + tarjeta + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tarjeta tarjeta) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta persistentTarjeta = em.find(Tarjeta.class, tarjeta.getNroTransaccion());
            Firma firmaOld = persistentTarjeta.getFirma();
            Firma firmaNew = tarjeta.getFirma();
            TipoTarjeta tipoOld = persistentTarjeta.getTipo();
            TipoTarjeta tipoNew = tarjeta.getTipo();
            Banco bancoOld = persistentTarjeta.getBanco();
            Banco bancoNew = tarjeta.getBanco();
            Pago pagoOld = persistentTarjeta.getPago();
            Pago pagoNew = tarjeta.getPago();
            List<String> illegalOrphanMessages = null;
            if (pagoNew != null && !pagoNew.equals(pagoOld)) {
                Tarjeta oldTarjetaOfPago = pagoNew.getTarjeta();
                if (oldTarjetaOfPago != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Pago " + pagoNew + " already has an item of type Tarjeta whose pago column cannot be null. Please make another selection for the pago field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (firmaNew != null) {
                firmaNew = em.getReference(firmaNew.getClass(), firmaNew.getIdFirma());
                tarjeta.setFirma(firmaNew);
            }
            if (tipoNew != null) {
                tipoNew = em.getReference(tipoNew.getClass(), tipoNew.getIdTipo());
                tarjeta.setTipo(tipoNew);
            }
            if (bancoNew != null) {
                bancoNew = em.getReference(bancoNew.getClass(), bancoNew.getIdBanco());
                tarjeta.setBanco(bancoNew);
            }
            if (pagoNew != null) {
                pagoNew = em.getReference(pagoNew.getClass(), pagoNew.getNroTransaccion());
                tarjeta.setPago(pagoNew);
            }
            tarjeta = em.merge(tarjeta);
            if (firmaOld != null && !firmaOld.equals(firmaNew)) {
                firmaOld.getTarjetaList().remove(tarjeta);
                firmaOld = em.merge(firmaOld);
            }
            if (firmaNew != null && !firmaNew.equals(firmaOld)) {
                firmaNew.getTarjetaList().add(tarjeta);
                firmaNew = em.merge(firmaNew);
            }
            if (tipoOld != null && !tipoOld.equals(tipoNew)) {
                tipoOld.getTarjetaList().remove(tarjeta);
                tipoOld = em.merge(tipoOld);
            }
            if (tipoNew != null && !tipoNew.equals(tipoOld)) {
                tipoNew.getTarjetaList().add(tarjeta);
                tipoNew = em.merge(tipoNew);
            }
            if (bancoOld != null && !bancoOld.equals(bancoNew)) {
                bancoOld.getTarjetaList().remove(tarjeta);
                bancoOld = em.merge(bancoOld);
            }
            if (bancoNew != null && !bancoNew.equals(bancoOld)) {
                bancoNew.getTarjetaList().add(tarjeta);
                bancoNew = em.merge(bancoNew);
            }
            if (pagoOld != null && !pagoOld.equals(pagoNew)) {
                pagoOld.setTarjeta(null);
                pagoOld = em.merge(pagoOld);
            }
            if (pagoNew != null && !pagoNew.equals(pagoOld)) {
                pagoNew.setTarjeta(tarjeta);
                pagoNew = em.merge(pagoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tarjeta.getNroTransaccion();
                if (findTarjeta(id) == null) {
                    throw new NonexistentEntityException("The tarjeta with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tarjeta tarjeta;
            try {
                tarjeta = em.getReference(Tarjeta.class, id);
                tarjeta.getNroTransaccion();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tarjeta with id " + id + " no longer exists.", enfe);
            }
            Firma firma = tarjeta.getFirma();
            if (firma != null) {
                firma.getTarjetaList().remove(tarjeta);
                firma = em.merge(firma);
            }
            TipoTarjeta tipo = tarjeta.getTipo();
            if (tipo != null) {
                tipo.getTarjetaList().remove(tarjeta);
                tipo = em.merge(tipo);
            }
            Banco banco = tarjeta.getBanco();
            if (banco != null) {
                banco.getTarjetaList().remove(tarjeta);
                banco = em.merge(banco);
            }
            Pago pago = tarjeta.getPago();
            if (pago != null) {
                pago.setTarjeta(null);
                pago = em.merge(pago);
            }
            em.remove(tarjeta);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tarjeta> findTarjetaEntities() {
        return findTarjetaEntities(true, -1, -1);
    }

    public List<Tarjeta> findTarjetaEntities(int maxResults, int firstResult) {
        return findTarjetaEntities(false, maxResults, firstResult);
    }

    private List<Tarjeta> findTarjetaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tarjeta.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tarjeta findTarjeta(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tarjeta.class, id);
        } finally {
            em.close();
        }
    }

    public int getTarjetaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tarjeta> rt = cq.from(Tarjeta.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
